'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 激活码
 * @author sundbcn@gmail.com (sun debing)
 */

var config_util = require('../util/configUtil');
var http_util = require('kc-http');

/**
 * 验证激活码
 * @param  {String}   key 激活码
 * @param  {Function} cb  回调
 * @return {Void}
 */
exports.checkCdkReward = function (key, cb) {
    var cdk = config_util.load('cdk');
    var param = {
        cdk_id: key
    };

    var onGet = function (err, data) {
        var cdk_package = {};
        /*jslint evil: true */
        var json_request = (new Function('return (' + data + ')')());
        if (json_request.code !== 10000) {
            return cb(null, cdk_package);
        }
        cdk_package.cdk_type = json_request.result.cdk_type;
        cdk_package.limit_times = json_request.result.limit_times;
        cdk_package.reward_list = JSON.parse(json_request.result.reward_list);
        cb(null, cdk_package);
    };
    http_util.post(cdk.host, param, onGet);
};

/**
 * 获取激活码类型
 * @param  {String}   key 激活码
 * @param  {Function} cb  回调
 * @return {Void}
 */
exports.getCdkTypeAndUseLimit = function (key, cb) {
    var cdk = config_util.load('cdk');
    var param = {
        cdk_id: key
    };

    var onGet = function (err, data) {
        var cdk_package = {};
        /*jslint evil: true */
        var json_request = (new Function('return (' + data + ')')());
        if (json_request.code !== 10000) {
            return cb(null, cdk_package);
        }
        cdk_package.cdk_type = json_request.result.cdk_type;
        cdk_package.limit_times = json_request.result.limit_times;
        cb(null, cdk_package);
    };
    http_util.post(cdk.verify_host, param, onGet);
};
